/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CdkPanelList.java
 *
 * Created on 09-mar-2009, 13:17:55
 */
package com.codeko.swing;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Codeko
 */
public class CdkPanelList extends javax.swing.JPanel {

    CdkPanelListModel model = null;
    DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
    ListDataListener listDataListener = new ListDataListener() {

        public void intervalAdded(ListDataEvent e) {
            for (int i = e.getIndex0(); i <= e.getIndex1(); i++) {
                JPanel p = getModel().getElementAt(i);
                panelDatos.add(p, i);
            }
        }

        public void intervalRemoved(ListDataEvent e) {
            for (int i = e.getIndex0(); i <= e.getIndex1(); i++) {
                panelDatos.remove(i);
            }
        }

        public void contentsChanged(ListDataEvent e) {
            panelDatos.updateUI();
        }
    };

    /** Creates new form CdkPanelList */
    public CdkPanelList() {
        initComponents();
        setModel(new CdkPanelListModel());
        this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                System.out.println(e.getSource().getClass().getName());
            //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    public CdkPanelListModel getModel() {
        return model;
    }

    public void setModel(CdkPanelListModel model) {
        if (this.model != null) {
            this.model.removeListDataListener(getListDataListener());
        }
        this.model = model;
        this.model.addListDataListener(getListDataListener());
    }

    public DefaultListSelectionModel getSelectionModel() {
        return selectionModel;
    }

    public void setSelectionModel(DefaultListSelectionModel selectionModel) {
        this.selectionModel = selectionModel;
    }

    public JScrollPane getScroll() {
        return scroll;
    }

    public ListDataListener getListDataListener() {
        return listDataListener;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scroll = new javax.swing.JScrollPane();
        panelDatos = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        panelDatos.setLayout(new java.awt.GridLayout(0, 1));
        scroll.setViewportView(panelDatos);

        add(scroll, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel panelDatos;
    private javax.swing.JScrollPane scroll;
    // End of variables declaration//GEN-END:variables
}
